package com.jhd.fuzhou.view.receipt;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.service.PunishmentService;
import com.jhd.fuzhou.service.ReceiveService;
import com.jhd.fuzhou.util.CustomToast;
import com.jhd.fuzhou.util.UtilHelper;
import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.vo.JsonForResult;
import com.jhd.fuzhou.vo.SysUserVo;

import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.StringUtil;

import java.util.ArrayList;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class ReadingCommentActivity extends BaseActivity {

    @BindView(R.id.et_suggestion)EditText et_suggestion;
    @BindView(R.id.iv_cancel)ImageView iv_cancel;
    private Context context=this;
    private String advise,id;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_reading_comment);
    }

    @Override
    protected void processLogic() {
        advise=getIntent().getStringExtra("advise");
        id=getIntent().getStringExtra("id");
        if(advise==null){
            et_suggestion.setText("已阅");
        }else{
            et_suggestion.setText(advise);
        }
        et_suggestion.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            @Override
            public void afterTextChanged(Editable s) {
                if("".equals(s.toString().trim())){
                    iv_cancel.setVisibility(View.GONE);
                }else{
                    iv_cancel.setVisibility(View.VISIBLE);
                }
            }
        });

    }
    @OnClick({R.id.btn_confirm,R.id.iv_back,R.id.iv_cancel})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.iv_cancel:
                et_suggestion.setText("");
                iv_cancel.setVisibility(View.GONE);
                break;
            case R.id.iv_back:
                onCancel();
                break;
            case R.id.btn_confirm:
                if(et_suggestion.getText().toString().trim().equals("")){
                    CustomToast.showText(context,"阅文意见不能为空!",3000,0,0).show();
                }else{
                    if(et_suggestion.getText().toString().trim().equals("已阅")){
                        CustomToast.showText(context,"请修改阅文意见!",3000,0,0).show();
                    }else{
                        addReadAdvise(id,et_suggestion.getText().toString());
                    }
                }
                break;
        }
    }
    public void addReadAdvise(String id,String advise){
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        ReceiveService vs = retrofit.create(ReceiveService.class);
        Map<String, String> params = ParamsMapUtil.getInstance("id",id).addParam("hasRead","1").addParam("userName",application.getUserName()).addParam("advise",advise);
        Call<JsonForResult> jsonForResultCall = vs.addReadAdvise(application.getAccessToken(),params);//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = (JsonForResult) response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        UtilHelper.alertMsg(context, "添加阅文意见成功!", jsonForResult.getMessage());
                        Intent intent = new Intent();
                        intent.putExtra("position","1");
                        ((Activity)context).setResult(RESULT_OK, intent);
                        ((Activity)context).finish();
                    } else {
                        UtilHelper.alertMsg(context, "错误", jsonForResult.getMessage());
                    }
                } catch (Exception e) {
                    CustomToast.showText(context,e.getLocalizedMessage(),3000,0,0).show();
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "网络异常!";
                if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg + t.getMessage();
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }

    public void onCancel() {
        Intent intent = new Intent();
        intent.putExtra("position","2");
        ((Activity)context).setResult(RESULT_OK, intent);
        ((Activity)context).finish();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onCancel();
        }
        return super.onKeyDown(keyCode, event);
    }
}